package com.extension.company.view.my.feedback;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.widget.GridSpacingItemDecoration;
import com.extension.company.contract.my.FeedBackContract;
import com.extension.company.presenter.my.FeedBackPresenter;
import com.extension.company.view.adapter.FeedBackAdapter;
import com.extension.company.view.adapter.FeedBackRecycleViewAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 问题反馈
 */
public class FeedBackActivity extends BaseTitleActivity<FeedBackContract.View, FeedBackPresenter>
		implements FeedBackContract.View, View.OnClickListener {

//	@BindView(R.id.mGridView)
//	GridView mGridView;

	@BindView(R.id.btn_submit)
	Button btn_submit;
	@BindView(R.id.feedback_rcy)
	RecyclerView feedbackRecycleView;
	private FeedBackAdapter adapter;
	private List<LocalMedia> selectList = new ArrayList<>();
	private FeedBackRecycleViewAdapter adapter1;

	public static void startActivity(Activity activity) {
		Intent intent = new Intent(activity, FeedBackActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("问题反馈");
		initView();


	}

	private void initView() {
		feedbackRecycleView.setLayoutManager(new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false));
		feedbackRecycleView.addItemDecoration(new GridSpacingItemDecoration(4, 12, false));
		adapter1 = new FeedBackRecycleViewAdapter(this, onAddPicClickListener);
		adapter1.setList(selectList);
		adapter1.setSelectMax(4); // 最多显示4个图片
		feedbackRecycleView.setAdapter(adapter1);
		adapter1.setOnItemClickListener(new FeedBackRecycleViewAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(int position, View v) {  // 点击显示图片的大图
				if (selectList.size() > 0) {
					LocalMedia media = selectList.get(position);
					String pictureType = media.getPictureType(); // 获取点击位置的类型
					if (!TextUtils.isEmpty(pictureType)) { // 返回的类型是图片
						PictureSelector.create(FeedBackActivity.this).themeStyle(R.style.picture_default_style).openExternalPreview(position, selectList);
					}
				}
			}
		});
	}

	@Override
	protected void initListener() {
		btn_submit.setOnClickListener(this);
	}

	@Override
	public int getMainLayout() {
		return R.layout.feedback;
	}

	@Override
	public FeedBackContract.View createView() {
		return this;
	}

	@Override
	public FeedBackPresenter createPresenter() {
		return new FeedBackPresenter();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.btn_submit:
//				presenter.
				FeedBackSuccessActivity.startActivity(this);
				finish();
				break;
			default:
				break;
		}
	}

	private FeedBackRecycleViewAdapter.onAddPicClickListener onAddPicClickListener = new FeedBackRecycleViewAdapter.onAddPicClickListener() {
		@Override
		public void onAddPicClick() {

			// 进入相册 以下是例子：不需要的api可以不写
			PictureSelector.create(FeedBackActivity.this)
					.openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
					.theme(R.style.picture_Sina_style)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style
					.maxSelectNum(4)// 最大图片选择数量
					.minSelectNum(1)// 最小选择数量
					.imageSpanCount(4)// 每行显示个数
					.selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选
					.previewImage(true)// 是否可预览图片
					.previewVideo(false)// 是否可预览视频
					.enablePreviewAudio(false) // 是否可播放音频
					.isCamera(true)// 是否显示拍照按钮
					.isZoomAnim(true)// 图片列表点击 缩放效果 默认true
					//.imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
					//.setOutputCameraPath("/CustomPath")// 自定义拍照保存路径
					.enableCrop(false)// 是否裁剪
					.compress(false)// 是否压缩
					.synOrAsy(true)//同步true或异步false 压缩 默认同步
					//	.compressSavePath(getPath())//压缩图片保存地址
					//.sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
					.glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
					.withAspectRatio(1, 1)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
					.hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
					.isGif(false)// 是否显示gif图片
					.freeStyleCropEnabled(true)// 裁剪框是否可拖拽
					.circleDimmedLayer(false)// 是否圆形裁剪
					.showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false
					.showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
					.openClickSound(false)// 是否开启点击声音
					.selectionMedia(selectList)// 是否传入已选图片
					.minimumCompressSize(100)// 小于100kb的图片不压缩
					.forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case PictureConfig.CHOOSE_REQUEST:
					selectList = PictureSelector.obtainMultipleResult(data);
					adapter1.setList(selectList);
					adapter1.notifyDataSetChanged();
					break;
			}
		}
	}

}
